package cn.bluemobi.dylan.step.presenter.my.fragment;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import cn.bluemobi.dylan.step.base.RxPresenter;
import cn.bluemobi.dylan.step.moder.bean.MemberAccount;
import cn.bluemobi.dylan.step.moder.bean.Score;
import cn.bluemobi.dylan.step.moder.bean.User;
import cn.bluemobi.dylan.step.moder.http.CommonSubscriber;
import cn.bluemobi.dylan.step.moder.http.RetrofitHelper;
import cn.bluemobi.dylan.step.moder.http.RxUtil;
import cn.bluemobi.dylan.step.moder.http.response.HttpResponseBean;
import cn.bluemobi.dylan.step.moder.http.response.PageDataBean;
import cn.bluemobi.dylan.step.moder.prefs.Prefs;
import cn.bluemobi.dylan.step.moder.prefs.SharedPreferencesHelper;
import cn.bluemobi.dylan.step.presenter.contract.my.ScoreListContract;
import cn.bluemobi.dylan.step.ui.my.activity.ScoreActivity;
import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by $Liudan on 2018/8/7 0007.
 */
public class ScoreListPresenter extends RxPresenter<ScoreListContract.View> implements ScoreListContract.Presenter {
    @Inject
    SharedPreferencesHelper prefHelper;
    private int currPage = 1;
    private int pageSize = 20;

    @Inject
    public ScoreListPresenter() {

    }

    /**
     * 获取user
     */
    public void preUser() {
        Observable<User> observable = Observable.create(new ObservableOnSubscribe<User>() {
            @Override
            public void subscribe(ObservableEmitter<User> emitter) throws Exception {
                User user = prefHelper.getPrefObject(Prefs.key_user, User.class);
                emitter.onNext(user);
                emitter.onComplete();
            }
        });
        //创建一个下游 Observer
        Observer<User> observer = new Observer<User>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(User user) {
                if (user != null) {
                    mView.showUserInfo(user);
                }
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        };
        //建立连接
        observable.subscribe(observer);

    }


    /**
     * 获取总积分
     */
    public void getMemberAccount(int memberId) {
        addSubscribe(RetrofitHelper.getLoginApi().getMemberAccount(memberId)
                .compose(RxUtil.<HttpResponseBean<MemberAccount>>rxSchedulerHelper())
                .compose(RxUtil.<MemberAccount>handleResult())
                .subscribeWith(new CommonSubscriber<MemberAccount>(mView) {
                    @Override
                    public void onNext(MemberAccount user) {
                        mView.showAccountScore(user);
                    }
                })
        );
    }

    List<Score> scoreList = new ArrayList<>();

    /**
     * 积分明细
     */
    public void httpScoreList(int memberId, boolean isRefresh,String tab) {
        if (isRefresh) {
            currPage = 1;
        }
        int status = 0; //1 收入 2 支出 不写是全部
        Flowable<HttpResponseBean<PageDataBean<List<Score>>>> flowable;
        if(ScoreActivity.tab2.equals(tab)){
            status = 1;
            flowable = RetrofitHelper.getLoginApi().getMemberAccountLogList(currPage, pageSize, memberId,status);
        }else if(ScoreActivity.tab3.equals(tab)){
            status = 2;
            flowable = RetrofitHelper.getLoginApi().getMemberAccountLogList(currPage, pageSize, memberId,status);
        }else {
            flowable = RetrofitHelper.getLoginApi().getMemberAccountLogList(currPage, pageSize, memberId);
        }

        addSubscribe(flowable
                .compose(RxUtil.<HttpResponseBean<PageDataBean<List<Score>>>>rxSchedulerHelper())
                .compose(RxUtil.<PageDataBean<List<Score>>>handleResult())
                .subscribeWith(new CommonSubscriber<PageDataBean<List<Score>>>(mView) {
                    @Override
                    public void onNext(PageDataBean<List<Score>> result) {
                        currPage = result.getCurrent();
                        if (currPage == 1) { //刷新
                            scoreList = result.getRecords();
                        } else {
                            scoreList.addAll(result.getRecords());
                        }
                        mView.httpScoreList(scoreList);
                        currPage++;
                    }

                    @Override
                    protected void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        mView.closeDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.closeDialog();
                    }
                })
        );
    }

}
